<#-- 注意事项：
    1. 相关页面需要引用时，请在文件头添加：<#include "/common/draw_car_select_by_shortlevel.ftl" />
    2. 引用页面对应的select必须为 车辆品牌、车系、排量、年份四种，且class要包含getCarInfoByShortLevel
    3. 引用页面对应的select必须增加data-value属性，赋值为品牌id、车系id、排量id或年份id
    4. 示例：
	车辆品牌：
    <select id="brandId" name="brandId" class="form-control select2me getCarInfoByShortLevel" value="${viewVo.brandId}">
	</select>
-->
<#import "/decorators/jscss.ftl" as jc />

<script type="text/javascript">
var typeLevels = [];
var cars = [];
$(document).ready(function(){
	console.warn("-------");
	//级联的车辆信息下拉框集合
	cars = $("select[class*='getCarInfoByShortLevel']");
	//此处获取车辆各级别的编码BEGIN
	$.ajax({
		url : cxjBasePath+"carmdm/reference/queryCarReferenceShortLevel.json",
		type : "post",
		async : false,
		dataType : "json",
		success : function (data){
			if(data.status==ajaxStatusSuccess){
				if(data.content) {
					var content = data.content;
					typeLevels = data.content;
				}
			}
		}
	});
	console.warn("typeLevels:"+JSON.stringify(typeLevels));
	//END
	if(cars.length != 0){
		init();
	}
	
	
});

function reload(){
	cars = $("select[class*='getCarInfoByShortLevel']");
	init();
}

//此处品牌下拉框的初始化信息BEGIN
function init(){
	var param = {};
	for(var a=0;a<cars.length;a++){
		//参数赋值
		param["param"+a] = cars.eq(a).data("value");
	}
	console.warn("初始化下拉框："+JSON.stringify(param));
	$.ajax({
		url : cxjBasePath+"carmdm/reference/initCarReferenceListByShortLevel.json",
		type : "post",
		async : false,
		dataType : "json",
		data : {"param":JSON.stringify(param)},
		success : function (data){
			if(data.status==ajaxStatusSuccess){
				if(data.content) {
					for(var a=0;a<cars.length;a++){
						var obj = cars.eq(a);
						var content = data.content[a];
						if(a == cars.length - 1){
							for(var i = 0 ;i<content.length;i++){
								if(cars.eq(a).data("value") == content[i]["carID"]){
									obj.append("<option selected=\"selected\" value = " + content[i]["carID"] + ">" + content[i]["refDetailName"] + "</option>");
									$("#s2id_"+obj.attr("id")).find("span[class='select2-chosen']").html(content[i]["refDetailName"]);
								}else{
									obj.append("<option value = " + content[i]["carID"] + ">" + content[i]["refDetailName"] + "</option>");
								}
							}
						}else{
							for(var i = 0 ;i<content.length;i++){
								if(obj.data("value") == content[i]["refDetailId"]){
									obj.append("<option selected=\"selected\" value = " + content[i]["refDetailId"] + ">" + content[i]["refDetailName"] + "</option>");
									$("#s2id_"+obj.attr("id")).find("span[class='select2-chosen']").html(content[i]["refDetailName"]);
								}else{
									obj.append("<option value = " + content[i]["refDetailId"] + ">" + content[i]["refDetailName"] + "</option>");
								}
							}
						}
					}
				}
			}
		}
	});
	
	//END
	for(var b=0;b<cars.length-1;b++){
		cars.eq(b).on("change",function(){
			getCar(this);
		});
	}
}

function getCar(this_){
	//当前下拉框所处位置
	var this_index = cars.index(this_);
	//当前下拉框的子下拉框所处位置
	var next_index = this_index + 1;
	//当前下拉框的子下拉框编码
	var typeLevel = typeLevels[next_index];
	var url = cxjBasePath+"carmdm/reference/queryCarReferenceListByShortLevel.json";
	if(typeLevel == ""){
		return;
	}
	var param = {};
 	param["typeLevel"] = typeLevel;
	for(var a=0;a<cars.length;a++){
		//参数赋值
		param["param"+a] = cars.eq(a).val();
		//子下拉框格式化
		if(a > this_index){
			cars.eq(a).html("<option value=\"\" selected=\"selected\">请选择</option>");
			cars.eq(a).select2();
		}
	}
	$.ajax({
		url : url,
		type : "get",
		async : false,
		dataType : "json",
		data : {"param":JSON.stringify(param)},
		success : function (data){
			if(data.status==ajaxStatusSuccess){
				if(data.content) {
					var obj = cars.eq(next_index);
					var content = data.content;
					if(next_index == cars.length - 1){
						for(var i = 0 ;i<content.length;i++){
							obj.append("<option value = " + content[i]["carID"] + ">" + content[i]["refDetailName"] + "</option>");
						}
					}else{
						for(var i = 0 ;i<content.length;i++){
							obj.append("<option value = " + content[i]["refDetailId"] + ">" + content[i]["refDetailName"] + "</option>");
						}
					}
				}
			}
		}
	});
 }
</script>
